package controls;

import gameAppLayer.enums.Constants;
import gameAppLayer.stateMachine.strategyState.mapElements.SystemTileViewBuilder;
import tiles.HexMeshBuilder2;

import com.jme3.asset.AssetManager;
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.renderer.RenderManager;
import com.jme3.renderer.ViewPort;
import com.jme3.renderer.queue.RenderQueue.Bucket;
import com.jme3.scene.Geometry;
import com.jme3.scene.Mesh;
import com.jme3.scene.Spatial;
import com.jme3.texture.Texture;

public class SystemTileViewControl extends AbstractViewStateMachineControl {

	public SystemTileViewControl(AssetManager assetManager) {
		super(assetManager, 2);		
	}
	
	public String[] defineStates() {
		String[] states = {
				"Default",
				"Highlighted"
		};
		
		return states;
	}
	
	public void defineSpatialStates() {
		materials[0] = null;
		textures[0] = null;		
		HexMeshBuilder2 hexBuild = new HexMeshBuilder2();
		meshes[0] = hexBuild.buildMesh();
		
		
		materials[1] = null;
		textures[1] = null;
		meshes[1] = meshes[0];
		
		//etc...
		
		
	}		
	
	public GameControl cloneForSpatial(Spatial spatial) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void resetToDefault() {
		changeState("Default");		
	}

	@Override
	protected void controlUpdate(float tpf) {
		if (currState.equalsIgnoreCase("Default")) {
			//do nothing
		}
		else if (currState.equalsIgnoreCase("Highlighted")) {
			//animate highlight
		}
		else {
			changeState("Default"); //if we ever, somehow, get into an unknown state, revert to default state
		}
	}

	@Override
	protected void controlRender(RenderManager rm, ViewPort vp) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void changeState(String newState)  {
		if (viewStates.contains(newState)) {
			if (newState.equalsIgnoreCase("Default")) {
				//load default
				HexMeshBuilder2 hexBuild = new HexMeshBuilder2();
				Mesh mesh = hexBuild.buildMesh();
				
				Geometry geometry = (Geometry) spatial;
				
				geometry.setMesh(mesh);
				Material mat = new Material(assetManager, Constants.unshadedMaterial);
				mat.setColor("Color", new ColorRGBA(0,0,0,0.0f));
				//mat.getAdditionalRenderState().setBlendMode(BlendMode.Alpha);
				geometry.setMaterial(mat);
				geometry.setQueueBucket(Bucket.Transparent);
				this.currState = "Default";
			}
			else if (newState.equalsIgnoreCase("Highlighted")) {
				//load highlight graphics
				
				
				
				
				this.currState = "Highlighted";
			}	
			else {
				changeState("Default");
				this.currState = "Default";
			}
			
			
			
		}		
	}

	

	

}
